﻿using OneOfAll.Common.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace OneOfAll.Common.Bussiness
{
    public class BehavoirHelper
    {
        private static BehavoirHelper _Instance = new BehavoirHelper();
        public static BehavoirHelper Instance
        {
            get { return _Instance; }
        }

        public List<Point> GetMonsterFollowPath(ScreenModel screen, MonsterModel monster)
        {
            List<Point> path = new List<Point>();
            if (monster.AttachPeople != null)
            {
                int srcX = (int)(monster.X + Global.GlobalResource.GridSize / 10.0);
                int srcY = (int)(monster.Y + Global.GlobalResource.GridSize / 10.0);
                int desX = (int)(monster.AttachPeople.X + Global.GlobalResource.GridSize / 10.0);
                int desY = (int)(monster.AttachPeople.Y + Global.GlobalResource.GridSize / 10.0);
                List<Point> list = screen.GetPath(srcX, srcY, desX, desY);
                path = list.Take(list.Count > monster.Speed / Global.GlobalResource.GridSize ? monster.Speed / Global.GlobalResource.GridSize : list.Count).ToList();
            }
            return path;
        }

        internal List<Point> GetMonsterPatrolPath(ScreenModel screen, MonsterModel monster)
        {
            List<Point> path = new List<Point>();

            Random rd = new Random(Guid.NewGuid().GetHashCode());
            byte flag = 0b0000;
            int x = (int)(monster.X + Global.GlobalResource.GridSize / 10.0) / Global.GlobalResource.GridSize;
            int y = (int)(monster.Y + Global.GlobalResource.GridSize / 10.0) / Global.GlobalResource.GridSize;
            int step = 0;
            while (flag != 0b1111 && step < monster.Speed / Global.GlobalResource.GridSize)
            {
                switch (rd.Next(4))
                {
                    case 0:
                        while (step < monster.Speed / Global.GlobalResource.GridSize)
                        {
                            if (screen.CanMove(x + 1, y))
                            {
                                x += 1;
                                path.Add(new Point(x * Global.GlobalResource.GridSize, y * Global.GlobalResource.GridSize));
                                step++;
                                flag = 0b0000;
                            }
                            else
                            {
                                flag += 0b0001;
                                break;
                            }
                        }
                        break;
                    case 1:
                        while (step < monster.Speed / Global.GlobalResource.GridSize)
                        {
                            if (screen.CanMove(x - 1, y))
                            {
                                x -= 1;
                                path.Add(new Point(x * Global.GlobalResource.GridSize, y * Global.GlobalResource.GridSize));
                                step++;
                                flag = 0b0000;
                            }
                            else
                            {
                                flag += 0b0010;
                                break;
                            }
                        }
                        break;
                    case 2:
                        while (step < monster.Speed / Global.GlobalResource.GridSize)
                        {
                            if (screen.CanMove(x, y + 1))
                            {
                                y += 1;
                                path.Add(new Point(x * Global.GlobalResource.GridSize, y * Global.GlobalResource.GridSize));
                                step++;
                                flag = 0b0000;
                            }
                            else
                            {
                                flag += 0b0100;
                                break;
                            }
                        }
                        break;
                    case 3:
                        while (step < monster.Speed / Global.GlobalResource.GridSize)
                        {
                            if (screen.CanMove(x, y - 1))
                            {
                                y -= 1;
                                path.Add(new Point(x * Global.GlobalResource.GridSize, y * Global.GlobalResource.GridSize));
                                step++;
                                flag = 0b0000;
                            }
                            else
                            {
                                flag += 0b1000;
                                break;
                            }
                        }
                        break;
                }
            }
            return path;
        }
    }


}
